package com.homegoods.login.ui.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.login.KGetVerHelper;
import com.homegoods.login.R;
import com.homegoods.login.databinding.LoginAuthCodeFragmentBindingImpl;
import com.homegoods.login.presenter.LoginSmsPresenter;
import com.homegoods.login.view.LoginSmsView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kedll.library.dialog.ECAlertDialog;
import com.kedll.library.fragment.KBaseFragment;
import com.kedll.library.util.KeyBoardUtils;
import com.kedll.library.util.MyLog;
import com.kedll.library.util.StringUtil;
import com.kedll.library.widget.AIAutoCompleteTextView;

import java.util.Objects;

/**
 * 验证码登录
 */
@Route(path = ARouterPath.PATH_AUTH_CODE)
public class AuthCodeFragment extends KBaseFragment<LoginAuthCodeFragmentBindingImpl, LoginSmsPresenter> implements LoginSmsView {
    private KGetVerHelper kGetVerHelper;
    private AIAutoCompleteTextView mPhone;

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent(Bundle savedInstanceState) {
        mBinding.setAuthCode(mPresenter);

        mPhone = mBinding.phone;

        RxTextView.afterTextChangeEvents(mBinding.inputVer)
                .compose(bindToLifecycle())
                .subscribe(textChangeEvent -> mBinding.btnLogin.setEnabled(
                        Objects.requireNonNull(textChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mPhone.getText())));

        RxTextView.afterTextChangeEvents(mBinding.phone)
                .compose(bindToLifecycle())
                .subscribe(textChangeEvent -> mBinding.btnLogin.setEnabled(
                        Objects.requireNonNull(textChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.inputVer.getText())));
    }



    @Override
    protected LoginSmsPresenter getPresenter() {
        kGetVerHelper = new KGetVerHelper(getActivity(), mBinding.getVer);
        return new LoginSmsPresenter(kGetVerHelper, this);
    }

    @Override
    protected int getLayout() {
        return R.layout.login_auth_code_fragment;
    }

    @Override
    public void hideKeyBoard() {
        KeyBoardUtils.hideKeyBoard(getActivity());
    }

    @Override
    public void showLoginView() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_LOGIN)
                .navigation(getActivity(), 100);
    }

    @Override
    public void haveNoVercorde() {
        ECAlertDialog dialog = ECAlertDialog.buildAlert(
                getActivity(),
                "如若您的手机号收不到短信验证码请致电" +
                        "客服，客服热线400-83-213423"
                , "取消"
                , "拨打"
                , null
                , (dialog1, which) -> {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:400-83-213423"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
        );
        dialog.show();
    }

    @Override
    public void setAutoTextViewAdapter(ArrayAdapter adapter) {

    }

    @Override
    public void showLoad(String msg) {
        MyLog.getInstance().showProgressDialog(getActivity(), msg);
    }

    @Override
    public void hideLoad() {
        MyLog.getInstance().dismissDialog();
    }
}
